﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MD5
{
    public partial class 文件MD5计算器 : Form
    {
        public 文件MD5计算器()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "文件| *.*";
            var res = ofd.ShowDialog();
            filePathTextBox1.Text = ofd.FileName;

            outTextBox1.Text = "";

            this.filePathTextBox1.Select(this.filePathTextBox1.Text.Length, 1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String fpath = filePathTextBox1.Text;
            if (!File.Exists(fpath))
            {
                MessageBox.Show("请选择正确的文件");
            }
            try
            {
                FileStream file = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] hash_byte = md5.ComputeHash(file);
                string str = System.BitConverter.ToString(hash_byte);
                str = str.Replace("-", "");
                outTextBox1.Text = str;
            }
            catch (Exception)
            {

            }
        }

        private void 文件MD5计算器_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.WindowsDefaultBounds;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "文件| *.*";
            var res = ofd.ShowDialog();
            filePathTextBox2.Text = ofd.FileName;

            outTextBox2.Text = "";
            this.filePathTextBox2.Select(this.filePathTextBox2.Text.Length, 1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String fpath = filePathTextBox2.Text;
            if (!File.Exists(fpath))
            {
                MessageBox.Show("请选择正确的文件");
            }
            try
            {
                FileStream file = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] hash_byte = md5.ComputeHash(file);
                string str = System.BitConverter.ToString(hash_byte);
                str = str.Replace("-", "");
                outTextBox2.Text = str;
            }
            catch (Exception)
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            String fpath1 = filePathTextBox1.Text;
            String fpath2 = filePathTextBox2.Text;

            label1.Text = "比对中";

            if (!File.Exists(fpath1))
            {
                MessageBox.Show("请选择正确的文件");
                return;
            }
            try
            {
                FileStream file = new FileStream(fpath1, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] hash_byte = md5.ComputeHash(file);
                string str = System.BitConverter.ToString(hash_byte);
                str = str.Replace("-", "");
                outTextBox1.Text = str;
            }
            catch (Exception)
            {

            }

            if (!File.Exists(fpath2))
            {
                MessageBox.Show("请选择正确的文件");
                return;
            }
            try
            {
                FileStream file = new FileStream(fpath2, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] hash_byte = md5.ComputeHash(file);
                string str = System.BitConverter.ToString(hash_byte);
                str = str.Replace("-", "");
                outTextBox2.Text = str;
            }
            catch (Exception)
            {

            }

            if (outTextBox1.Text == "" || outTextBox2.Text == "")
            {
                MessageBox.Show("11111");
            }
            else
            {
                if (outTextBox1.Text == outTextBox2.Text)
                {
                    label1.Text = "比对正确";
                    label1.ForeColor = Color.Green;
                }
                else
                {
                    label1.Text = "比对错误";
                    label1.ForeColor = Color.Red;
                }
            }
        }
    }
}
